class Mypromise {
    constructor(executor) {
        this.data = null
        this.status = "pending"
        this.resolveCallbacks = []
        this.rejectCallbacks = []
        try {
            executor(this.resolve.bind(this), this.reject.bind(this))
        } catch (e) {
            this.reject(e)
        }
    }
    resolve(value) {
        if (this.status === "pending") {
            this.data = value
            this.status = "resolved"
            for (const func in this.resolveCallbacks) {
                func(this.data)
            }
        }
    }
    reject(reason) {
        if (this.status === "pending") {
            this.data = reason
            this.status = "rejected"
            for (const func in this.rejectCallbacks) {
                func(this.data)
            }
        }
    }
    then(onResolved, onRejected) {
        onRejected = typeof onRejected === "function" ? onRejected : v => v
        onResolved = typeof onResolved === "function" ? onRejected : e => e
        let promise2 = new MyPromise((resolve, reject) => {
            // 箭头函数的this指向上一级作用域的this即promise1
            if (this.status === "pending") {
                this.resolveCallbacks.push(function () {
                    setTimeout(() => {
                        try {
                            const x = onResolved(this.data)
                            this.resolvePromise(prommise2, x, resolve, reject)
                        } catch (err) {
                            reject(err)
                        }
                    }, 0)
                })
                this.rejectCallbacks.push(function () {
                    setTimeout(() => {
                        try {
                            const x = onRejected(this.data)
                            this.resolvePromise(promise2, x, resolve, reject)
                        } catch (err) {
                            reject(err)
                        }
                    }, 0)
                })
            }
            if (this.status === "resolved") {
                setTimeout(() => {
                    try {
                        const x = onResolved(this.data)
                        this.resolvePromise(promise2, x, resolve, reject)
                    } catch (err) {
                        reject(err)
                    }
                })
            }
            if (this.status === "rejected") {
                setTimeout(() => {
                    try {
                        const x = onRejected(this.data)
                        this.resolvePromise(promise2, x, resolve, reject)
                    } catch (err) {
                        reject(err)
                    }
                })
            }

        })




    }
    resolvePromise(promise2, x, resolve, reject){
        let call = false
        if(x === promise2){
            return reject(new TypeError('Chaining cycle detected for promise'))
        }else if(x instanceof MyPromise){
            if(x.status === "pending"){
                x.then()
            }
        }
    }
}

    